package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.TrayProductFreezeDetailMapper;
import com.canaan.business.domain.bill.TrayProductFreezeDetail;
import com.canaan.business.service.bill.ITrayProductFreezeDetailService;
import com.canaan.common.annotation.TableName;
/**
 * 托盘物料冻结明细Service业务层处理
 *
 * @author canaan
 * @date 2025-04-21
 */
@Service
public class TrayProductFreezeDetailServiceImpl implements ITrayProductFreezeDetailService {
    @Resource
    private TrayProductFreezeDetailMapper trayProductFreezeDetailMapper;

    /**
     * 查询托盘物料冻结明细
     *
     * @param id 托盘物料冻结明细主键
     * @return 托盘物料冻结明细
     */
    @Override
    public TrayProductFreezeDetail selectTrayProductFreezeDetailById(Long id) {
        return trayProductFreezeDetailMapper.selectTrayProductFreezeDetailById(id);
    }

    /**
     * 查询托盘物料冻结明细列表
     *
     * @param trayProductFreezeDetail 托盘物料冻结明细
     * @return 托盘物料冻结明细
     */
    @Override
    public List<TrayProductFreezeDetail> selectTrayProductFreezeDetailList(TrayProductFreezeDetail trayProductFreezeDetail) {
        return trayProductFreezeDetailMapper.selectTrayProductFreezeDetailList(trayProductFreezeDetail);
    }

    /**
     * 根据单据号查询托盘物料明细集合
     */
    @Override
    public List<TrayProductFreezeDetail> selectTrayProductFreezeDetailByBillNo(String billNo){
        return trayProductFreezeDetailMapper.selectTrayProductFreezeDetailByBillNo(billNo);
    }

    /**
     * 根据托盘号查询托盘物料明细集合
     */
    @Override
    public List<TrayProductFreezeDetail> selectTrayProductFreezeDetailByTrayNo(String trayNo){
        return trayProductFreezeDetailMapper.selectTrayProductFreezeDetailByTrayNo(trayNo);
    }
    /**
     * 新增托盘物料冻结明细
     *
     * @param trayProductFreezeDetail 托盘物料冻结明细
     * @return 结果
     */
    @Override
    public int insertTrayProductFreezeDetail(TrayProductFreezeDetail trayProductFreezeDetail) {
        trayProductFreezeDetail.setCreateBy(SecurityUtils.getUsername());
        trayProductFreezeDetail.setCreateTime(DateUtils.getNowDate());
        return trayProductFreezeDetailMapper.insertTrayProductFreezeDetail(trayProductFreezeDetail);
    }

    /**
     * 批量新增托盘物料冻结明细
     *
     * @param trayProductFreezeDetailList 托盘物料冻结明细列表
     * @return 结果
     */
    @Override
    public int batchInsertTrayProductFreezeDetail(List<TrayProductFreezeDetail> trayProductFreezeDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (TrayProductFreezeDetail trayProductFreezeDetail : trayProductFreezeDetailList){
            trayProductFreezeDetail.setCreateBy(username);
            trayProductFreezeDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<trayProductFreezeDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > trayProductFreezeDetailList.size()) {
                endIndex = trayProductFreezeDetailList.size();
            }
            insertNum = insertNum + trayProductFreezeDetailMapper.batchInsertTrayProductFreezeDetail(trayProductFreezeDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改托盘物料冻结明细
     *
     * @param trayProductFreezeDetail 托盘物料冻结明细
     * @return 结果
     */
    @Override
    public int updateTrayProductFreezeDetail(TrayProductFreezeDetail trayProductFreezeDetail) {
        trayProductFreezeDetail.setUpdateBy(SecurityUtils.getUsername());
        trayProductFreezeDetail.setUpdateTime(DateUtils.getNowDate());
        return trayProductFreezeDetailMapper.updateTrayProductFreezeDetail(trayProductFreezeDetail);
    }

    /**
     * 批量修改托盘物料冻结明细
     *
     * @param trayProductFreezeDetailList 托盘物料冻结明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateTrayProductFreezeDetail(List<TrayProductFreezeDetail> trayProductFreezeDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (TrayProductFreezeDetail trayProductFreezeDetail : trayProductFreezeDetailList){
            trayProductFreezeDetail.setUpdateBy(username);
            trayProductFreezeDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<trayProductFreezeDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > trayProductFreezeDetailList.size()) {
                endIndex = trayProductFreezeDetailList.size();
            }
            updateNum = updateNum + trayProductFreezeDetailMapper.batchUpdateTrayProductFreezeDetail(trayProductFreezeDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除托盘物料冻结明细
     *
     * @param ids 需要删除的托盘物料冻结明细主键集合
     * @return 结果
     */
    @Override
    public int deleteTrayProductFreezeDetailByIds(Long[] ids) {
        return trayProductFreezeDetailMapper.deleteTrayProductFreezeDetailByIds(ids);
    }

    /**
     * 删除托盘物料冻结明细信息
     *
     * @param id 托盘物料冻结明细主键
     * @return 结果
     */
    @Override
    public int deleteTrayProductFreezeDetailById(Long id) {
        return trayProductFreezeDetailMapper.deleteTrayProductFreezeDetailById(id);
    }
}
